package com.hzhc.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzhc.entity.CompanyInfo;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.mapper.CompanyInfoMapper;
import com.hzhc.service.CompanyInfoService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.vo.CompanyInfoVo;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 客户信息 服务实现类
 * </p>
 *
 * @author 功不唐捐
 * @since 2024-05-23
 */
@RequiredArgsConstructor
@Service
public class CompanyInfoServiceImpl extends ServiceImpl<CompanyInfoMapper, CompanyInfo> implements CompanyInfoService {

    private final CompanyInfoMapper companyinfoMapper;

    @Override
    public List<CompanyInfoVo> getList(CompanyInfoVo vo, Page<CompanyInfoVo> pagination) {
        IPage<CompanyInfoVo> iPage = companyinfoMapper.getList(pagination, vo);
        return iPage.getRecords();
    }

    @Override
    public List<CompanyInfoVo> getCompanyInfoVoJson(Page<CompanyInfoVo> page, CompanyInfoVo vo) {
        IPage<CompanyInfoVo> iPage = companyinfoMapper.selectCompanyJson(page, vo);
        return iPage.getRecords();
    }

    @Override
    public ResultUtil<T> checkInfo(CompanyInfo info) {
        int i = count(Wrappers.<CompanyInfo>lambdaQuery()
                .eq(CompanyInfo::getDisable, 0)
                .ne(info.getId() != null, CompanyInfo::getId, info.getId())
                .eq(CompanyInfo::getName, info.getName()));
        if (i > 0) {
            return ResultUtil.build(ResultUtilCodeEnum.COMPANYINFO_NAME);
        }
        if (StrUtil.isNotBlank(info.getNameSimple())) {
            int j = count(Wrappers.<CompanyInfo>lambdaQuery()
                    .eq(CompanyInfo::getDisable, 0)
                    .ne(info.getId() != null, CompanyInfo::getId, info.getId())
                    .eq(CompanyInfo::getNameSimple, info.getNameSimple()));
            if (j > 0) {
                return ResultUtil.build(ResultUtilCodeEnum.COMPANYINFO_NAMESIMPLE);
            }
        }
        return ResultUtil.ok();
    }

    @Override
    public ResultUtil<T> addRet(CompanyInfo info) {
        info.setDisable(false).setCreateTime(LocalDateTime.now());
        boolean b = save(info);
        return b ? ResultUtil.ok() : ResultUtil.fail();
    }

    @Override
    public ResultUtil<T> updateRet(CompanyInfo info) {
        info.setUpdateTime(LocalDateTime.now());
        boolean b = updateById(info);
        return b ? ResultUtil.ok() : ResultUtil.fail();
    }

    @Override
    public ResultUtil<T> updateDelete(Long id) {
        boolean b = update(Wrappers.<CompanyInfo>lambdaUpdate()
                .eq(CompanyInfo::getId, id)
                .set(CompanyInfo::getDisable, 1));
        return b ? ResultUtil.ok() : ResultUtil.fail();
    }

    @Override
    public List<CompanyInfo> getDriverCompanyList() {
        return baseMapper.getDriverCompanyList();
    }

    @Override
    public List<CompanyInfoVo> getCompanyList(CompanyInfoVo vo) {
        return baseMapper.getCompanyList(vo);
    }
}
